package net.yb.controller;

import lombok.RequiredArgsConstructor;
import net.yb.entity.User;
import net.yb.service.UserService;
import net.yb.service.impl.UserServiceImpl;
import net.yb.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 控制层
 */
// @ResponseBody
// @Controller
@RestController // 等价于 @Controller + @ResponseBody
@RequiredArgsConstructor
public class UserController {

    // @Autowired // 自动注入
    // @Resource
    private final UserService userService;

    @Value("${user.address}")
    private String address;

    @RequestMapping("/test")
    public String test(){
        userService.test();
        return "Success -----> " + address;
    }

    @RequestMapping("/login")
    public AjaxResult login(){
        String username = "tom";
        String password = "123";

        User user = userService.login(username, password);

        // AjaxResult result = new AjaxResult();
        // result.setStatus(200);
        // result.setMsg("登录成功");
        // result.setData(user);

        return AjaxResult.success(200, "登录成功", user);
    }

    @RequestMapping("/register")
    public AjaxResult register(){
        User user = new User();
        user.setUsername("tom");
        user.setPassword("123");
        user.setAge(18);
        user.setSex("男");
        user.setHeight(180.6);

        userService.register(user);

        // AjaxResult result = new AjaxResult();
        // result.setStatus(200);
        // result.setMsg("注册成功");

        return AjaxResult.success();
    }
}
